﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using Utility;
using System.IO;

namespace hpm
{
    public partial class frmDangKy : DevExpress.XtraEditors.XtraForm
    {

        clsNgheNghiep objNgheNghiep = new clsNgheNghiep();
        Utility utility = new Utility();
        public bool isEdit = false;
        public string MaBenhNhan = "";


        public frmDangKy()
        {
            InitializeComponent();
        }

        private void frmDangKy_Load(object sender, EventArgs e)
        {
            if (!isEdit)
                this.LoadThemMoi();
            else
                this.LoadSua();
        }

        private void LoadThemMoi()
        {
            InitDefaultData();
        }


        private void LoadSua()
        {
            clsBenhNhan objBenhNhan = new clsBenhNhan();
            clsQuocTich objQuocTich = new clsQuocTich();
            clsDanToc objDanToc = new clsDanToc();
            clsTinh objTinh = new clsTinh();
            clsDoiTuong objDoiTuong = new clsDoiTuong();

            objBenhNhan.MaBenhNhan = this.MaBenhNhan;
            objBenhNhan = objBenhNhan.GetBenhNhanById();
            DateTime dateNgaySinh = Convert.ToDateTime(objBenhNhan.NgaySinh);

            txtMaSoThe.Text = objBenhNhan.MaBenhNhan;
            dateNgayCap.EditValue = objBenhNhan.NgayTao;
            txtHoten.Text = objBenhNhan.HoTen;

            if (objBenhNhan.GioiTinh == "Nam")
                rdNam.Checked = true;
            else
                rdNu.Checked = true;

            lkNgay.EditValue = dateNgaySinh.Day;
            lkThang.EditValue = dateNgaySinh.Month;
            txtNamSinh.Text = dateNgaySinh.Year.ToString();
            txtDiaChi.Text = objBenhNhan.DiaChi;

            //InitTinh(objBenhNhan.MaTinh);
            HpmUtility.InitLookupEdit(lkTinh, objTinh.getAllTinh(), "TenTinh", "MaTinh", objBenhNhan.MaTinh);

            //InitQuanHuyen(objBenhNhan.MaHuyen);
            HpmUtility.InitLookupEdit(lkQuanHuyen, objBenhNhan.MaHuyen);
            
            txtCMND.Text = objBenhNhan.CMND;
            txtDienThoai.Text = objBenhNhan.DienThoai;
            
            //Nghe Nghiep
            HpmUtility.InitLookupEdit(lkNgheNghiep, objNgheNghiep.Get_NgheNghiep(), "TenNghe", "MaNghe", objBenhNhan.MaNghe);

            //Quoc tich
            HpmUtility.InitLookupEdit(lkQuocTich, objQuocTich.getAllQuocTich(), "TenQuocTich", "MaQuocTich", objBenhNhan.MaQuocTich);

            txtNoiLamViec.Text = objBenhNhan.NoiLamViec;
            txtEmail.Text = objBenhNhan.Email;
            txtNguonGioiThieu.Text = objBenhNhan.NoiLamViec;
            txtNT_HoTen.Text = objBenhNhan.NT_HoTen;
            txtNT_DienThoai.Text = objBenhNhan.NT_DienThoai;
            txtNT_QuanHe.Text = objBenhNhan.NT_QuanHe;
            txtNT_DiaChi.Text = objBenhNhan.NT_DiaChi;

            //Dan Toc
            HpmUtility.InitLookupEdit(lkDanToc, objDanToc.getAllDanToc(), "DanToc", "MaDanToc", objBenhNhan.MaDanToc);

            //Doi Tuong
            HpmUtility.InitLookupEdit(lkDoiTuong, objDoiTuong.getAllDoiTuong(), "TenDoiTuong", "MaDoiTuong", objBenhNhan.MaDoiTuong);

            //Load Hinh Anh
            if (objBenhNhan.HinhAnh.Length != 0)
                HpmUtility.LoadImgeTo(imgHinhAnh, objBenhNhan.HinhAnh);

        }


        private void InitQuanHuyen(string quanValue = "")
        {
            lkQuanHuyen.EditValue = lkQuanHuyen.Properties.GetKeyValueByDisplayText(quanValue);
        }


        /// <summary>
        /// Make some default Data
        /// </summary>
        private void InitDefaultData()
        {
            clsBenhNhan objBenhNhan = new clsBenhNhan();
            clsQuocTich objQuocTich = new clsQuocTich();
            clsDanToc objDanToc = new clsDanToc();
            clsTinh objTinh = new clsTinh();
            clsDoiTuong objDoiTuong = new clsDoiTuong();
            clsNgheNghiep objNgheNghiep = new clsNgheNghiep();

            txtMaSoThe.Text = HpmUtility.BuildNewId(objBenhNhan.getMaxId());
            dateNgayCap.EditValue = HpmUtility.strNgayHienTai;
            txtHoten.Select();

            HpmUtility.InitLookupEdit(lkTinh, objTinh.getAllTinh(), "TenTinh", "MaTinh", objTinh.getMacDinh());
            HpmUtility.InitLookupEdit(lkNgheNghiep, objNgheNghiep.Get_NgheNghiep(), "TenNghe", "MaNghe", objNgheNghiep.getMacDinh());
            HpmUtility.InitLookupEdit(lkQuocTich, objQuocTich.getAllQuocTich(), "TenQuocTich", "MaQuocTich", objQuocTich.getMacDinh());
            HpmUtility.InitLookupEdit(lkDanToc, objDanToc.getAllDanToc(), "DanToc", "MaDanToc", objDanToc.getMacDinh());
            HpmUtility.InitLookupEdit(lkDoiTuong, objDoiTuong.getAllDoiTuong(), "TenDoiTuong", "MaDoiTuong", objDoiTuong.getMacDinh());
            
            HpmUtility.InitLookupEdit(lkDanToc, objDanToc.getAllDanToc(), "DanToc", "MaDanToc");
        }


        /// <summary>
        /// Save Object
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtNamSinh.Text == "")
            {
                MessageBox.Show("Nhập năm sinh của bệnh nhân");
                return;
            }

            if (txtHoten.Text == "")
            {
                MessageBox.Show("Nhập ho ten bệnh nhân");
                return;
            }

            if (HpmUtility.strNgayHienTai == "")
            {
                HpmUtility.strNgayHienTai = utility.getNgayHientai();
            }

            //clsBenhNhan.AddBenhNhan();
            clsBenhNhan objBenhNhan = new clsBenhNhan();

            objBenhNhan.MaBenhNhan = txtMaSoThe.Text;

            if (!isEdit)
                objBenhNhan.NgayTao = HpmUtility.strNgayHienTai;
            else
            {
                objBenhNhan.NgayTao = dateNgayCap.EditValue.ToString();
                objBenhNhan.NgaySua = HpmUtility.strNgayHienTai;
            }

            objBenhNhan.HoTen = txtHoten.Text.Trim();
            objBenhNhan.NgaySinh = txtNamSinh.Text + "-" + lkThang.SelectedItem.ToString() + "-" + lkNgay.SelectedItem.ToString();
            objBenhNhan.GioiTinh = (rdNam.Checked) ? "Nam" : "Nu";
            objBenhNhan.MaNghe = lkNgheNghiep.GetColumnValue("MaNghe").ToString();
            objBenhNhan.MaDanToc = lkDanToc.GetColumnValue("MaDanToc").ToString();
            objBenhNhan.MaDoiTuong = lkDoiTuong.GetColumnValue("MaDoiTuong").ToString();
            objBenhNhan.DienThoai = txtDienThoai.Text.Trim();
            objBenhNhan.MaQuocTich = lkQuocTich.GetColumnValue("MaQuocTich").ToString();
            objBenhNhan.DiaChi = txtDiaChi.Text.Trim();
            objBenhNhan.MaTinh = lkTinh.GetColumnValue("MaTinh").ToString();
            objBenhNhan.MaHuyen = lkQuanHuyen.GetColumnValue("MaQuan").ToString();
            objBenhNhan.NoiLamViec = txtNoiLamViec.Text.Trim();
            objBenhNhan.Email = txtEmail.Text.Trim();
            objBenhNhan.NguonGioiThieu = txtNguonGioiThieu.Text.Trim();
            //objBenhNhan.MaThe = 
            objBenhNhan.CMND = txtCMND.Text.Trim();
            objBenhNhan.NT_HoTen = txtNT_HoTen.Text.Trim();
            objBenhNhan.NT_DienThoai = txtNT_DienThoai.Text.Trim();
            objBenhNhan.NT_QuanHe = txtNT_QuanHe.Text.Trim();
            objBenhNhan.NT_DiaChi = txtNT_DiaChi.Text.Trim();

            Byte[] imgData = new ImageConverter().ConvertTo(imgHinhAnh.Image, typeof(Byte[])) as Byte[];
            objBenhNhan.HinhAnh = imgData;

            if(isEdit)
                objBenhNhan.saveBenhNhan(txtMaSoThe.Text);
            else
                objBenhNhan.saveBenhNhan();
        }


        /// <summary>
        /// LookupEdit Tinh select changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lkTinh_EditValueChanged(object sender, EventArgs e)
        {
            clsQuanHuyen objQuanHuyen = new clsQuanHuyen();
            objQuanHuyen.MaTinh = lkTinh.EditValue.ToString();
            lkQuanHuyen.Properties.DataSource = objQuanHuyen.getQuanHuyenByTinh();
            lkQuanHuyen.Properties.DisplayMember = "TenQuan";
            lkQuanHuyen.Properties.ValueMember = "MaQuan";

            if (isEdit)
                InitQuanHuyen();
        }

        /// <summary>
        /// TextBox Nam Sinh changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtNamSinh_EditValueChanged(object sender, EventArgs e)
        {
            txtTuoi.Text = (2013 - int.Parse((txtNamSinh.Text == "") ? "0" : txtNamSinh.Text)).ToString();
        }


        /// <summary>
        /// Textbox Tuoi changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtTuoi_EditValueChanged(object sender, EventArgs e)
        {
            txtNamSinh.Text = (DateTime.Now.Year - int.Parse((txtTuoi.Text == "") ? "0" : txtTuoi.Text)).ToString();
        }
    }
}